
/**
 * Date:2019年5月31日下午3:00:50
 * Copyright (c) 2019, www.bwbroad.com All Rights Reserved.
 *
*/

package com.xue.im.common.coder;

import java.io.IOException;

import org.jboss.marshalling.ByteInput;
import org.jboss.marshalling.Unmarshaller;

import io.netty.buffer.ByteBuf;

/**
 * Description: TODO <br/>
 * Date: 2019年5月31日 下午3:00:50 <br/>
 * 
 * @author xuejianxin
 */
public class MyMarshallingDecoder {
	private final Unmarshaller unmarshaller;

	public MyMarshallingDecoder() throws IOException {
		this.unmarshaller = MarshallingCodeCFactory.buildUnmarshaller();
	}

	public Object decode(ByteBuf in) throws Exception {
		int objectSize = in.readInt();
		ByteBuf buf = in.slice(in.readerIndex(), objectSize);
		ByteInput input = new MyChannelBufferByteInput(buf);

		try {
			unmarshaller.start(input);
			Object obj = unmarshaller.readObject();
			unmarshaller.finish();
			in.readerIndex(in.readerIndex()+objectSize);
			return obj;
		} finally {
			unmarshaller.close();
		}
	}

}
